package com.gewe.wxChat.job.once;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gewe.wxChat.api.base.LoginApi;
import com.gewe.wxChat.model.entity.SysConfig;
import com.gewe.wxChat.model.enums.SysConfigEnum;
import com.gewe.wxChat.service.SysConfigService;
import com.gewe.wxChat.utils.OkhttpUtil;
import com.gewe.wxChat.utils.SanZhiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;

/**
 * 设置回调地址
 */
@Component
public class WxChatApiInit implements CommandLineRunner {
    
    @Resource
    private SysConfigService sysConfigService;
    
    private final static Logger logger= LoggerFactory.getLogger(WxChatApiInit.class);
    
    @Override
    public void run(String... args) throws Exception {
        String trueToken = getWxChatApiToken();
        String sanZhiToken = getSanZhiToken();
        //设置gewe token
        Class<?> clazz = OkhttpUtil.class;
        OkhttpUtil okhttpUtil= (OkhttpUtil) clazz.newInstance();
        Field token = clazz.getDeclaredField("token");
        token.setAccessible(true);
        token.set(okhttpUtil, trueToken);
        //设置llk Token
        Class<?> sanZhiClazz = SanZhiUtils.class;
        SanZhiUtils sanZhiUtils= (SanZhiUtils) sanZhiClazz.newInstance();
        Field sanZhitoken = sanZhiClazz.getDeclaredField(SysConfigEnum.LLK_TOKEN.getValue());
        sanZhitoken.setAccessible(true);
        sanZhitoken.set(sanZhiUtils, sanZhiToken);
        
        logger.info("gewetoken初始化完成\n\n"+"token:"+(String) token.get(okhttpUtil));
    }
    private  String getWxChatApiToken() {
        SysConfig sysConfig = sysConfigService.getOne(
                new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getObjectType, SysConfigEnum.GEWE_TOKEN.getValue()));
        if (sysConfig != null ) {
            //取出时间戳
            String[] split = sysConfig.getObjectValue().split("\\|");
            //比较时间戳是否大于当前时间
            long expireTime=Long.parseLong(split[0]);
            String token = split[1];
            if(expireTime>new Date().getTime()){
                return token;
            }
        }else {
            sysConfig=new SysConfig();
        }
        JSONObject jsonObject = LoginApi.getToken();
        if ((Integer) jsonObject.get("ret") == 200 && jsonObject.get("msg").equals("执行成功")) {
            sysConfig.setObjectType(SysConfigEnum.GEWE_TOKEN.getValue());
            //value使用时间戳拼接token
            long expireTime = new Date().getTime()+2*3600;
            sysConfig.setObjectValue(expireTime+"|"+jsonObject.getString("data").toString());
        }
        sysConfigService.saveOrUpdate(sysConfig);
        return jsonObject.getString("data").toString();
    }

    private String getSanZhiToken(){
        SysConfig sysConfig=sysConfigService.getOne(
                new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getObjectType, SysConfigEnum.LLK_TOKEN.getValue()));
        return sysConfig.getObjectValue();
    }
}
